<template>
	<section>
		<!--工具条-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form :inline="true" :model="filters">
				<el-form-item>
					<el-input v-model="filters.keyword" placeholder="关键字"></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" v-on:click="search">查询</el-button>
				</el-form-item>
			</el-form>
		</el-col>

		<!--列表
		   @selection-change="selsChange" 选的不一样就会触发

		-->
		<el-table :data="acquisitionOrders" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
			<el-table-column type="selection" width="55">
			</el-table-column>
			<el-table-column type="index" width="60">
			</el-table-column>
			<el-table-column prop="orderSn" label="订单号" width="160" sortable>
			</el-table-column>
			<el-table-column prop="price" label="价格" width="160" sortable>
			</el-table-column>
			<!--:formatter="formatState"-->
			<el-table-column prop="state" label="状态" width="150"  sortable>
				<template scope="scope">
					<span style="color: green" v-if="scope.row.state==0">待处理</span>
					<span style="color: red" v-else-if="scope.row.state==-1">取消</span>
					<span style="color: darkblue" v-else>完成</span>
				</template>
			</el-table-column>
			<!--<el-table-column prop="digest" label="摘要" width="160" sortable>-->
			<!--</el-table-column>-->
			<el-table-column prop="address" label="地址" width="200" sortable>
			</el-table-column>
			<el-table-column label="操作">
				<template scope="scope">
					<el-button size="small" @click="handleConfirm(scope.$index, scope.row)">订单确认</el-button>
					<el-button type="danger" size="small" @click="handleCancel(scope.$index, scope.row)">取消</el-button>
				</template>
			</el-table-column>
		</el-table>

		<!--工具条-->
		<el-col :span="24" class="toolbar">
			<!--如果没有选中就不能使用:disabled="this.sels.length===0"-->
			<el-button type="danger" :disabled="this.sels.length===0">批量删除</el-button>
			<el-pagination layout="prev, pager, next" :current-page="page" @current-change="handleCurrentChange" :page-size="pageSize" :total="total" style="float:right;">
			</el-pagination>
		</el-col>

		<!--新增或编辑界面-->
		<el-dialog title="确认订单" :visible.sync="acquisitionOrderFormVisible" :close-on-click-modal="false">
			<!--:model="acquisitionOrderForm 模型绑定-->
			<!--:rules="acquisitionOrderFormRules" 校验规则-->
			<!--ref="acquisitionOrderForm" 给该表单定义一个引用名-->
			<el-form :model="acquisitionOrderForm" label-width="80px" :rules="acquisitionOrderFormRules" ref="acquisitionOrderForm">
				<el-form-item label="支付方式" prop="payType">
					<el-select v-model="acquisitionOrderForm.payType" placeholder="请选择">
						<el-option
								v-for="item in payTypeOptions"
								:key="item.value"
								:label="item.label"
								:value="item.value">
						</el-option>
					</el-select>
				</el-form-item>
				<el-form-item label="支付金额" prop="money">
					<el-input v-model="acquisitionOrderForm.money" style="width: 200px" auto-complete="off"></el-input>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="acquisitionOrderFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="confirmOrder" :loading="acquisitionOrderLoading">确认</el-button>
			</div>
		</el-dialog>

	</section>
</template>

<script>
	export default {
		data() {
			return {
				filters: {
					keyword: ''
				},
				acquisitionOrders: [],
				total: 0,
				page: 1,
				pageSize:10,
				listLoading: false,
				sels: [],//列表选中列

				acquisitionOrderFormVisible: false,//新增界面是否显示
                acquisitionOrderLoading: false,
                acquisitionOrderFormRules: {
				},
                acquisitionOrderForm: {
                    id:null,
					payType: '',
					money: ''
				},
                //支持三种选项
                payTypeOptions: [{
                    value: 0,
                    label: '余额'
                }, {
                    value: 1,
                    label: '垫付'
                }, {
                    value: 2,
                    label: '银行转账'
                }],
			}
		},
		methods: {
            search() {
                //查询完，原来分页条件已经没用了，要跑到第一页去
                this.page = 1;
                this.getAcquisitionOrders();
            }
			,
			handleCurrentChange(val) {
				this.page = val;
				this.getAcquisitionOrders();
			},
			//获取收购订单列表
			getAcquisitionOrders() {
				let para = {
					currentPage: this.page,
					pageSize:this.pageSize,
					keyword: this.filters.keyword
				};
				this.listLoading = true;
				this.$http.post("/acquisitionOrder",para)
					.then(result=>{
					    result = result.data; //取出data这一层
                        this.total = result.total;
                        this.acquisitionOrders = result.data;
                        this.listLoading = false;
					})
					.catch(result=>{
					    alert("系统错误！")
					})
			},
			//显示编辑界面
            handleConfirm: function (index, row) {
                //重置
                this.acquisitionOrderForm={
                    id:null,
                        payType: '',
                        money: ''
                }
					//设置id
                this.acquisitionOrderForm.id = row.id;
                this.acquisitionOrderFormVisible = true; // 显示编辑框
			},
			//保存
            confirmOrder: function () {
                // 表单上定义了ref="acquisitionOrderForm"
				this.$refs.acquisitionOrderForm.validate((valid) => {
					if (valid) {
						this.$confirm('确认订单吗？', '温馨提示', {}).then(() => {
							this.acquisitionOrderLoading = true;
							//拷贝一份新数据
							let para = Object.assign({}, this.acquisitionOrderForm);
							this.$http.post("/acquisitionOrder/confirm",para)
								.then(result => {
									result = result.data
									//去除忙等框
									this.acquisitionOrderLoading = false;
									//做提示
									if(result.success){
										this.$message({
											message: '操作成功',
											type: 'success'
										});
									}else{
										this.$message({
											message: result.message,
											type: 'error'
										});
									}
									//关闭对话框
									this.acquisitionOrderFormVisible =false;
									//重新查询数据
									this.getAcquisitionOrders();
								})
								.catch(result=>{
									alert("系统错误！");
                                    this.acquisitionOrderLoading = false;
								})
							;
						});
					}
				});
			},
			//所有选中对象都会传进来
			selsChange: function (sels) {
				this.sels = sels;
				console.log(this.sels)
			},
            //删除
            handleCancel: function (index, row) {
                this.$confirm('确认取消该订单吗?', '温馨提示', {
                    type: 'warning'
                }).then(() => {
                    this.listLoading = true;
                    let url = "/acquisitionOrder/"+row.id;
                    this.$http.delete(url)
                        .then(result=>{
                                result = result.data; //{sucess:false,message}
                                //去除忙等框
                                this.listLoading = false;

                                if(result.success){
                                    this.page = 1;
                                    //提示
                                    this.$message({
                                        message: '取消成功',
                                        type: 'success'
                                    });
                                    //重新查询数据
                                    this.getAcquisitionOrders();
                                }else{
                                    //提示
                                    this.$message({
                                        message: result.message,
                                        type: 'error'
                                    });
                                    //重新查询数据
                                    this.getAcquisitionOrders();
                                }
                            }

                        )
                        .catch(result=>{
                            //去除忙等框
                            this.listLoading = false;
                            //提示
                            this.$message({
                                message: '系统错误！',
                                type: 'error'
                            });
                        })

                }).catch(() => {

                });
            }
		},
		mounted() {
			this.getAcquisitionOrders();
		}
	}

</script>

<style scoped>

</style>